クロス表におけるセル毎の平均を計算
data(warpbreaks)
head(warpbreaks)
## breaks wool tension
## 1 26 A L
## 2 30 A L
## 3 54 A L
## 4 25 A L
## 5 70 A L
## 6 52 A L
attach(warpbreaks)
brks <- cut(breaks, breaks = c(0, 30, 70))
table(brks, wool) # クロス表
## wool
## brks A B
## (0,30] 18 21
## (30,70] 9 6
n1 <- length(levels(brks)) # 行数
n2 <- length(levels(wool)) # 列数
mlab <- list(brks = levels(brks), wool = levels(wool)) # ラベル
matrix(tapply(breaks, brks:wool, mean), n1, n2, dimnames = mlab) # セル毎の平均
## wool
## brks A B
## (0,30] 21.89 49.33
## (30,70] 21.24 39.33
matrix(tapply(breaks, brks:wool, sd), n1, n2, dimnames = mlab) # セル毎の標準偏差
## wool
## brks A B
## (0,30] 6.144 13.096
## (30,70] 5.567 4.502
detach(warpbreaks)